package com.core;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegsService {
	
	private static Regs regs;
	static {
		regs = new Regs();
	}
	
	/**
	 * <p>Title: getInfo</p>
	 * <p>Description: </p>
	 * @param regName ： 正则表达式在com.core.Regs中对应的属性名称
	 * @param str：		被处理的字符串
	 * @return	String：
	 * 				找不到相应的子串：	null
	 * 				能找到对应的子串：	用`分割的“value”。（数据文件中不存在`）
	 * @throws Exception
	 */
	public String getInfo(String regName, String str) throws Exception {
		Pattern pattern = regs.get(regName);
		Matcher matcher = pattern.matcher(str);
		String res = "";
		while(matcher.find()) {
			String temp = "null".equals(matcher.group(0).split("=")[1]) ? "" : matcher.group(0).split("=")[1];
			if(temp != null && !"".equals(temp))
				res += "`" + temp;
		}
		return res.equals("") ? null : res.substring(1, res.length());
	}
	
	/**
	 * <p>Title: getInfo</p>
	 * <p>Description: </p>
	 * @param regName ： 正则表达式在com.core.Regs中对应的属性名称
	 * @param str：		被处理的字符串
	 * @return	String：
	 * 				找不到相应的子串：	null
	 * 				能找到对应的子串：	用`分割的“key=value”。（数据文件中不存在`）
	 * @throws Exception
	 */
	public String getFullInfo(String regName, String str) throws Exception {
		Pattern pattern = regs.get(regName);
		Matcher matcher = pattern.matcher(str);
		String res = "";
		while(matcher.find()) {
			res += "`" + matcher.group(0);
		}
		return res.equals("") ? null : res.substring(1, res.length());
	}
	/**
	 * <p>Title: printInfos</p>
	 * <p>Description: 输出map信息</p>
	 * @param map
	 */
	public void printInfos(Map<String, PortGuidance> map) {
		System.out.println(map.size());
		Set<String> keySet = map.keySet();
		if (keySet.size() != 0) {
			Iterator<String> iterator = keySet.iterator();
			while (iterator.hasNext()) {
				PortGuidance guidance = map.get(iterator.next());
				if (guidance.getFlno() != null && guidance.getTarget() != null) {
					System.out.print("航班号：" + guidance.getFlno());
					System.out.print("\t\t共享航班号："+guidance.getSfno());
					System.out.print("\t\t经停站：" + guidance.getStopover());
					System.out.println("\t\t目的地：" + guidance.getTarget());
					String fett = guidance.getFett();
					if(fett != null)
						System.out.print("预计起飞时间：" + fett.substring(0, 4) + "年" + fett.substring(4, 6) + "月" + fett.substring(6, 8) + "日" + fett.substring(8, 10) + "时" + fett.substring(10, 12) + "分");
					else
						System.out.print("预计起飞时间：" + "未知");
					
					String frtt = guidance.getFrtt();
					if(frtt != null)
						System.out.println("\t\t\t\t实际起飞时间：" + frtt.substring(0, 4) + "年" + frtt.substring(4, 6) + "月" + frtt.substring(6, 8) + "日" + frtt.substring(8, 10) + "时" + frtt.substring(10, 12) + "分");
					else
						System.out.println("\t\t\t\t实际起飞时间：" + "未知");
					
					String code = guidance.getCode();
					if(code == null || "null".equals(code))
						code = "未知";
					else
						code = code.replaceAll("`", "或");
					System.out.print("航班状态：" + guidance.getStatus());
					System.out.println("\t\t值班柜台：" + code);
					System.out.println("---------------------------------------------------------------------------------");
				}
			}
		}
	}
	
	// 截取消息类型
	public String getType(String str) {
		return str.substring(20, 29);
	}
	
}